package mall.controller.admin;

import mall.core.jdbc.Page;
import mall.core.web.Controller;
import mall.entity.Book;
import mall.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/admin/book")
public class AdminBookController extends Controller {

    private final BookService bookService = new BookService();

    @Override
    protected Book getParameterForEntity(HttpServletRequest request) {
        Book book = new Book();

        String id = request.getParameter("id");
        if (id != null && !id.isEmpty()) {
            book.setId(Integer.valueOf(id));
        }
        String bookName = request.getParameter("bookName");
        if (bookName != null && !bookName.isEmpty()) {
            book.setBookName(String.valueOf(bookName));
        }


        return book;
    }

    /**
     * 新增
     */
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book book = getParameterForEntity(request);

        bookService.add(book);

        redirect("/admin/book?method=viewList", request, response);
    }

    /**
     * 删除
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = getParameterForEntity(request);
        bookService.deleteById(book.getId());
        redirect("/admin/book?method=viewList", request, response);
    }

    /**
     * 修改
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = getParameterForEntity(request);
        bookService.update(book);

        redirect("/admin/book?method=viewList", request, response);
    }

    /**
     * 查询
     */
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book book = getParameterForEntity(request);
        Page<Book> page = getParameterForPage(request);

        bookService.page(page, book);

        request.setAttribute("id", book.getId());
        request.setAttribute("bookName", book.getBookName());

        setPageAttribute(request, page);

        forward("/admin/book/list", request, response);
    }
    /**
     * 访问列表页面
     */
    public void viewList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = getParameterForEntity(request);
        Page<Book> page = getParameterForPage(request);

        bookService.page(page, book);

        setPageAttribute(request, page);

        forward("/admin/book/list", request, response);
    }

    /**
     * 访问新增页面
     */
    public void viewCreate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward("/admin/book/create", request, response);
    }

    /**
     * 访问编辑页面
     */
    public void viewEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = getParameterForEntity(request);
        book = bookService.getById(book.getId());
        request.setAttribute("book", book);
        forward("/admin/book/edit", request, response);
    }
}
